import QtQuick 1.0
import com.nokia.symbian 1.0
import QtMobility.location 1.2

Page {
    id: page1
    tools: toolBarNavPage
    anchors.fill: parent

    PositionSource {
        id: posSource
        active: false
        updateInterval: 1000

        onPositionChanged: {
            mapItem.center = position.coordinate;
            myPosition.center = position.coordinate;
            myPosition.visible = true;
        }
    }

    Flickable {
        id: flickableMap
        anchors.fill: parent
        contentHeight: 2000
        contentWidth: 1000
        contentX: (contentWidth - width) / 2
        contentY: (contentHeight - height) / 2

        Map {
            id: mapItem
            anchors.fill: parent

            zoomLevel: 10
            center: Coordinate {
                latitude: 61.4979781
                longitude: 23.7649307
            }

            mapType: Map.StreetMap
            plugin: Plugin {
                name: "nokia"
            }
            MapCircle {
                id: myPosition
                visible: false
                border.color: "red"
                border.width: 5
                radius: 500
                center: Coordinate { // Tampere
                    latitude: 61.4979781
                    longitude: 23.7649307
                }
            }
        }
    }

    ButtonRow {
        width: parent.width - platformStyle.paddingLarge
        anchors.horizontalCenter: parent.horizontalCenter
        Button {
            text: "Zoom In"
            onClicked: mapItem.zoomLevel += 1;
        }
        Button {
            text: "Zoom Out"
            onClicked: mapItem.zoomLevel -= 1;
        }
    }

    ToolBarLayout {
        id: toolBarNavPage
        ToolButton {
            flat: true
            iconSource: "toolbar-back"
            onClicked: pageStack.depth <= 1 ? Qt.quit() : pageStack.pop()
        }
        ToolButton {
            flat: true
            iconSource: "toolbar-menu"
            onClicked: navOptionsMenu.open()
        }
    }

    Menu {
        id: navOptionsMenu
        content: MenuLayout {
            MenuItem {
                text: "Start Tracking"
                onClicked: posSource.start()
            }
            MenuItem {
                text: "Stop Tracking"
                onClicked: posSource.stop()
            }
            MenuItem {
                text: "Set Position"
                onClicked: mapCoordDialog.open()
            }
            MenuItem {
                text: "Streep Map"
                onClicked: mapItem.mapType = Map.StreetMap
            }
            MenuItem {
                text: "Satellite Map"
                onClicked: mapItem.mapType = Map.SatelliteMapDay
            }
            MenuItem {
                text: "Terrain Map"
                onClicked: mapItem.mapType = Map.TerrainMap
            }
        }
    }

    CommonDialog {
        id: mapCoordDialog
        titleText: "Enter Coordinates"
        anchors {
            verticalCenter: parent.verticalCenter
            left: parent.left
            leftMargin: 10
        }
        height: parent.height / 2
        buttons: ToolBar {
            id: buttons
            width: parent.width
            tools: Row {
                anchors.centerIn: parent
                spacing: platformStyle.paddingMedium
                ToolButton {
                    text: "Ok"
                    width: (buttons.width - 3 * platformStyle.paddingMedium) / 2
                    onClicked: {
                        mapItem.center.latitude = latitudeField.text;
                        mapItem.center.longitude = longitudeField.text;
                        mapCoordDialog.accept();
                    }
                }
                ToolButton {
                    text: "Cancel"
                    width: (buttons.width - 3 * platformStyle.paddingMedium) / 2
                    onClicked: mapCoordDialog.reject()
                }
            }
        }
        content: Column {
            anchors.fill: parent
            spacing: platformStyle.paddingMedium
            Text {
                text: "Enter Latitude"
                color: platformStyle.colorNormalLight
                font.pixelSize: 16
            }
            TextField {
                id: latitudeField
                width: parent.width
                inputMethodHints: ImhFormattedNumbersOnly
            }
            Text {
                text: "Enter Longitude"
                color: platformStyle.colorNormalLight
                font.pixelSize: 16
                horizontalAlignment: Text.AlignHCenter
            }
            TextField {
                id: longitudeField
                width: parent.width
            }

        }
    }
}


